package lab;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.service.jdbc.connections.spi.AbstractMultiTenantConnectionProvider;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;

public class MultiTenantConnectionProviderImpl extends
        AbstractMultiTenantConnectionProvider {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private ConnectionProvider fdbProvider = null;// ConnectionProviderUtils.
    // buildConnectionProvider
    // ("acme");
    private ConnectionProvider medeqProvider = null;// ConnectionProviderUtils
    // .
    // buildConnectionProvider
    // ("jboss");

    {
        DataSourceConnectionProvider d1 = new DataSourceConnectionProvider("medeq");
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("url", "jdbc:sqlserver://RD2K5SQL:1433");
        map1.put("userName", "medeqtest");
        map1.put("password", "medeq123");
        d1.configure(map1);
        medeqProvider = d1;

        DataSourceConnectionProvider d2 = new DataSourceConnectionProvider("fdb");
        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("url", "jdbc:sqlserver://RD2K5SQL:1433");
        map2.put("userName", "fdb");
        map2.put("password", "fdb");
        d2.configure(map2);
        fdbProvider = d2;
    }

    @Override
    protected ConnectionProvider getAnyConnectionProvider() {
        return fdbProvider;
    }

    @Override
    protected ConnectionProvider selectConnectionProvider(
            String tenantIdentifier) {
        if ("fdb".equals(tenantIdentifier)) {
            return fdbProvider;
        } else if ("medeq".equals(tenantIdentifier)) {
            return medeqProvider;
        }
        throw new HibernateException("Unknown tenant identifier");
    }
}
